package com.common.controller;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.servlet.ServletException;

import com.common.constant.BalzLogger;

public class BalzMD5Generate {
	
	BalzLogger logger = new BalzLogger();
	
	public String generatePasswordMD5(String password) throws ServletException {
			
		byte[] defaultBytes = password.getBytes();
		
		try{
			MessageDigest algorithm = MessageDigest.getInstance("MD5");
			algorithm.reset();
			algorithm.update(defaultBytes);
			byte messageDigest[] = algorithm.digest();
		            
			StringBuffer hexString = new StringBuffer();
			for (int i=0;i<messageDigest.length;i++) {
				hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
			}
			
			logger.debug("sessionid "+password+" md5 version is "+hexString.toString());
			
			password=hexString+"";
			
		}catch(NoSuchAlgorithmException algorithmException){
			
		}
		
		return password;
	}
	
//	public static void main(String[] args) {
//		BalzMD5Generate generate = new BalzMD5Generate();
//		try {
//			System.out.println(">> "+generate.generatePasswordMD5("1234"));
//		} catch (ServletException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
}
